<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Jobs\OrderSubmit;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Carbon\Carbon;
use App\Repository\UpThree\MainOrderRepostition;

class TelecomGDAController extends BaseController
{
  public function __construct()
  {
    $this->log = new Logger('TelecomGD');
    $this->log->pushHandler(
      (new RotatingFileHandler(storage_path('/logs/TelecomGD.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
    );
  }
  // 电信那边推送过来的数据 
  public function pushOrderStatus(Request $Request)
  {
    $getData = file_get_contents('php://input');
    $this->log->info('input' . ($getData));
    // $editData = [
    //   'status' => 50,
    //   'three_msg' => '多通道失败单',
    // ];
    // $where = ['orderno' => 'HK_20220827233051133491860740'];
    // $model = new MainOrderRepostition();
    // $res = $model->followOrderStatus($where, $editData);
    // die();
    // return '接受成功';
    // var_dump(1111111111);
    // $getData = '{"logisticsCompanies":"顺丰快递","sign":"64B92D7B8B8D027CCB52D5574068C3F9","phoneNum":"F95662BD5703BBDB8AA03793A08E8EDA","ordId":"Hk_20220521170443374284738314","orderStatusDescribe":"订单完成","orderState":"D1002","requestTime":1653235229000,"areaCode":"0399","balanceOne":"20000.0","identityId":"B0E29D818D5B4EA9848EA75EB4F4D91DFEF0D36AED32723F0DCF2577C22A7D1B","name":"029CE5B6800B31E3DECAC5493CE768E7","uim":"5674320039F2CA563E9C5FCC112B970F9411186656B275C747F1B7262C45D3DF","logisticsNo":"4BED77E946003D610086CEA5B264535F","state":"100000","describe":"激活成功","stateDate":1653216306000}';
    // $getData = '{"订单编码":"220801142218rtwgtttt","订单当前状态":{"订单状态ID":37,"订单状态名称":"配货异常中","状态备注":"黎铸财要求异常","状态更新时间":"2022-08-01T17:55:23+08:00"},"办理业务":{"业务类型":"赋能产品业务","办理类型":"新装主卡"},"订购移动电话":[{"移动接入号":"18926252449","UIM实物串号":""}],"生成运单":[{"运单号":"1297334375760"}],"asyn_notice_data":{"order_code":"220801142218rtwgtttt","order_remark":"黎铸财要求异常","status_id":37,"status_name":"配货异常中","status_time":"2022-08-01T17:55:23+08:00"}}';
    // $getData = '{"订单编码":"220729092835xtspattt","订单当前状态":{"订单状态ID":32,"订单状态名称":"待下单人修改","状态备注":"生成赋能平台订单失败用户身份证号校验不通过;详情：【IDENTITY】国政通公安系统：库中无此号！","状态更新时间":"2022-08-01T12:12:15+08:00"},"办理业务":{"业务类型":"赋能产品业务","办理类型":"新装主卡"},"订购移动电话":[{"移动接入号":"","UIM实物串号":""}],"asyn_notice_data":{"order_code":"220729092835xtspattt","order_remark":"生成赋能平台订单失败用户身份证号校验不通过;详情：【IDENTITY】国政通公安系统：库中无此号！","status_id":32,"status_name":"待下单人修改","status_time":"2022-08-01T12:12:15+08:00"}}';
    //   $getData = '{
    //     "订单编码":"220801142240eturwttt",
    //     "订单当前状态":{
    //         "订单状态ID":36,
    //         "订单状态名称":"物流配货中",
    //         "状态备注":"订单推送到物流公司成功",
    //         "状态更新时间":"2022-08-01T15:49:50+08:00"
    //     },
    //     "办理业务":{
    //         "业务类型":"赋能产品业务",
    //         "办理类型":"新装主卡"
    //     },
    //     "订购移动电话":[
    //         {
    //             "移动接入号":"18924029482",
    //             "UIM实物串号":""
    //         }
    //     ],
    //     "生成运单":[
    //         {
    //             "运单号":"1297334544960"
    //         }
    //     ],
    //     "asyn_notice_data":{
    //         "order_code":"220801142240eturwttt",
    //         "order_remark":"订单推送到物流公司成功",
    //         "status_id":36,
    //         "status_name":"物流配货中",
    //         "status_time":"2022-08-01T15:49:50+08:00"
    //     }
    // }';
    // 数据处理  
    if (empty($getData)) {
      return json_encode(['responseCode' => 1, 'result' => '未查询到数据']);
    }
    $getData = json_decode($getData, true);
    if (empty($getData)) {
      return json_encode(['responseCode' => 1, 'result' => '未查询到数据']);
    }
    $datas = [];
    if (!empty($getData['asyn_notice_data'])) {
      $datas = [
        'order_code' => !empty($getData['asyn_notice_data']['order_code']) ? $getData['asyn_notice_data']['order_code'] : (!empty($getData['asyn_notice_data']['订单编码']) ? $getData['asyn_notice_data']['订单编码'] : ''),
        'status_id' => !empty($getData['asyn_notice_data']['status_id']) ? $getData['asyn_notice_data']['status_id'] : '',
        'order_remark' => !empty($getData['asyn_notice_data']['order_remark']) ? $getData['asyn_notice_data']['order_remark'] : (!empty($getData['订单作废']['作废详细原因']) ? $getData['订单作废']['作废详细原因'] : ''),
        'status_name' => !empty($getData['asyn_notice_data']['status_name']) ? $getData['asyn_notice_data']['status_name'] : '',
        'status_time' => !empty($getData['asyn_notice_data']['status_time']) ? date('Y-m-d H:i:s', strtotime($getData['asyn_notice_data']['status_time'])) : (!empty($getData['asyn_notice_data']['订单状态更新时间']) ? date('Y-m-d H:i:s', strtotime($getData['asyn_notice_data']['订单状态更新时间'])) : ''),


      ];
      if (empty($datas['status_id'])) {
        $datas['status_id'] = $getData['订单当前状态']['订单状态ID'];
      }
      if (!empty($getData['生成运单'][0]['运单号'])) {
        $datas['express_number'] = $getData['生成运单'][0]['运单号'];
      }
      if (!empty($getData['订单配送方式']['配送公司'])) {
        $datas['express_company'] = $getData['订单配送方式']['配送公司'];
      }
      if (!empty($getData['订购移动电话'][0]['移动接入号'])) {
        $datas['iccid'] = $getData['订购移动电话'][0]['移动接入号'];
      }
      if (!empty($getData['订购移动电话'][0]['UIM实物串号'])) {
        $datas['card_num'] = $getData['订购移动电话'][0]['UIM实物串号'];
      }
    }
    if (empty($datas['order_code'])) {
      return '订单号未查询出';
    }
    if (empty($datas)) {
      return '接受成功';
    }
    $status = 10;
    switch ($datas['status_id']) {
      case 0:
        $status = 40;
        break;
      case 36:
        $status = 20;
        break;
      case 38:
        $status = 20;
        break;
      case 32:
        $status = 60;
        break;
      case 58:
        $status = 20;
        break;
      case 39:
        $status = 20;
        break;
      case 84:
        $status = 20;
        break;
      case 1:
        $status = 60;
        break;
    }
    // 将数据格式 修改成固定的数据格式
    $editData = [
      'status' => $status,
      'express_company' => !empty($datas['express_company']) ? $datas['express_company'] : '',
      'express_number' => !empty($datas['express_number']) ? $datas['express_number'] : '',
      'three_msg' => $datas['order_remark'],
      'iccid' => !empty($datas['iccid']) ? $datas['iccid'] : '',
      'first_status' => !empty($datas['balanceOne']) ? '1' : '0', // 是否首冲
      'first_status_desc' => !empty($datas['balanceOne']) ? ($datas['balanceOne'] / 100) : '0', // 首冲金额
    ];
    $where = ['sup_orderno' => $datas['order_code']];
    $model = new MainOrderRepostition();
    $res = $model->followOrderStatus($where, $editData);
    $this->log->info('电信通知参数' . json_encode($getData) . '返回参数' . json_encode($res));
    if (!empty($res['code']) && $res['code'] == 200) {
      return json_encode(['responseCode' => 0, 'result' => '接受成功']);
    }
    return json_encode(['responseCode' => 1, 'result' => '数据处理失败']);
  }
}
